<template>
  <div class="jioninfo-dialog">
    <el-dialog
      v-model="dialogVisible"
      @open="onOpen"
      @close="onClose"
      title="评价详情"
    >
      <el-row>
        <el-col :span="12">用户名：{{ props.currentEvaluate.userName }}</el-col>
        <el-col :span="12"
          >所在活动：{{ props.currentEvaluate.proName }}</el-col
        >
      </el-row>
      <el-row>
        <el-col :span="12"
          >指导老师：{{ props.currentEvaluate.teacherName }}</el-col
        >
        <el-col :span="12">所属机构：{{ props.currentEvaluate.unit }}</el-col>
      </el-row>
      <el-table
        v-loading="loading"
        :data="stageList"
        @cell-click="showScoreInput"
      >
        <el-table-column
          label="阶段"
          align="center"
          prop="missionName"
          width=""
        />
        <el-table-column label="成果" align="center" prop="" width="">
          <template #default="scope">
            <span
              @click="openResult(scope.row)"
              v-if="scope.row.id != 'last'"
              style="cursor: pointer"
              >查看</span
            >
            <span
              @click="downloadAllResult(scope.row)"
              v-else-if="stageList.length > 1"
              style="cursor: pointer"
              >下载</span
            >
            <span v-else>暂无</span>
          </template>
        </el-table-column>
        <el-table-column label="评分" align="center" prop="score" width="">
          <template #default="{ row, column }">
            <el-input
              v-show="tableRowEditId === row.id && row.id != 'last'"
              @blur="blurScoreInput(row, column)"
              @keyup.enter="blurScoreInput(row, column)"
              v-model="row.score"
              :ref="
                (el) => {
                  inputRefList[row.id] = el;
                }
              "
            />
            <el-tooltip
              content="点击可修改分数"
              placement="top"
              v-if="tableRowEditId != row.id && row.id != 'last'"
            >
              <span>{{ row.score }}</span>
            </el-tooltip>
            <span v-if="tableRowEditId != row.id && row.id == 'last'">{{
              row.score
            }}</span>
          </template>
        </el-table-column>
      </el-table>
      <el-row>
        <el-col :span="2">评价：</el-col>
        <el-col :span="22">
          <el-input
            v-model="evaluateText"
            type="textarea"
            placeholder="请输入评价"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-col>
      </el-row>
      <template #footer>
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </template>
    </el-dialog>
    <StageDialog
      v-model="dialogResultVisible"
      :currentStage="currentStage"
      :userId="props.currentEvaluate.userId"
    ></StageDialog>
  </div>
</template>
<script setup>
import useActivityStore from "@/store/modules/activity";
import { getToken } from "@/utils/auth";
import { getEvaluateInfo, getEvaluateZip } from "@/api/pms/project";
import StageDialog from "./stageDialog.vue";
import { saveAs } from "file-saver";
import { blobValidate } from "@/utils/ruoyi";

const headers = ref({
  Authorization: "Bearer " + getToken(),
});
const baseURL = import.meta.env.VITE_APP_BASE_API;
const { proxy } = getCurrentInstance();
const props = defineProps({
  currentEvaluate: { type: Object, default: {} },
});
const formRef = ref();
const ruleFormRef = ref();
const stageList = ref([]);
const loading = ref(false);
const currentStage = ref({});
const dialogResultVisible = ref(false);
const evaluateText = ref("");
const tableRowEditId = ref(null);
const inputRefList = ref({});

// 弹窗设置
const dialogVisible = defineModel();
// 弹窗确认回调
const emit = defineEmits(["confirm"]);
/**
 * @name: 弹窗打开后执行
 * @description: 弹窗打开后执行方法
 * @return {*}
 */
async function onOpen() {
  getList();
}
//修改时初始化
function getList() {
  loading.value = true;
  let params = {
    projectId: props.currentEvaluate.proId,
    userId: props.currentEvaluate.userId,
  };
  getEvaluateInfo(params).then((response) => {
    stageList.value = response.data.stageList;
    evaluateText.value = response.data.evaluate;
    loading.value = false;
  });
}
/**
 * @name: 弹窗关闭时执行
 * @description: 弹窗关闭方法，重置表单
 * @return {*}
 */
function onClose() {
  evaluateText.value = "";
  stageList.value = [];
  tableRowEditId.value = null;
}
/**
 * @name: 弹窗取消
 * @description: 弹窗取消方法
 * @return {*}
 */
function close() {
  dialogVisible.value = false;
}
/**
 * @name: 弹窗表单提交
 * @description: 弹窗表单提交方法
 * @return {*}
 */
function handelConfirm() {
  // TODO 提交表单
  // 回调父级组件
  let params = {
    evaList: JSON.parse(JSON.stringify(stageList.value)),
    evaluate: evaluateText.value,
    projectId: props.currentEvaluate.proId,
    userId: props.currentEvaluate.userId,
  };
  emit("confirm", params);
  close();
}
//打开阶段详情
function openResult(row) {
  currentStage.value = JSON.parse(JSON.stringify(row));
  dialogResultVisible.value = true;
}
//下载最终成果zip
function downloadAllResult(row) {
  let params = {
    projectId: props.currentEvaluate.proId,
    userId: props.currentEvaluate.userId,
  };
  getEvaluateZip(params)
    .then(async (res) => {
      let fileName = decodeURI(
        res.headers["content-disposition"].split("filename*=UTF-8''")[1]
      );
      let data = res.data;
      const isBlob = blobValidate(data);
      if (isBlob) {
        const blob = new Blob([data]);
        saveAs(blob, fileName);
      } else {
        const resText = await data.text();
        const rspObj = JSON.parse(resText);
      }
    })
    .catch((r) => {
      console.error(r);
    });
}
//点击分数单元格
function showScoreInput(row, column) {
  // console.log('row', row)
  // console.log('column', column)
  //赋值给定义的变量
  if (row.id != "last") {
    nextTick(() => {
      inputRefList.value[row.id].focus();
    });
    if (column.property == "score") {
      tableRowEditId.value = row.id; //确定点击的单元格在哪行
    }
  }
}
//修改分数单元格
function blurScoreInput(row, column) {
  tableRowEditId.value = null;
  //在此处调接口传数据
}
</script>
<style lang="scss">
.el-upload__tip {
  line-height: 1.2;
}
.file-tip {
  padding-left: 100px;
  color: red;
  margin-bottom: 5px;
  margin-top: 12px;
  display: inline-block;
}
.jioninfo-dialog {
  .el-dialog {
    .el-dialog__body {
      height: 70vh;
      overflow-y: auto;
      .el-row {
        padding: 10px 0;
      }
    }
  }
}
</style>
